package com.example.threaddemo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * jdk 自带的线程池使用
 */
public class JDKThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool =
                Executors.newFixedThreadPool(5);
//        Executors.newSingleThreadExecutor();
        Executors.newCachedThreadPool();
        //获取 cpu 的核数
        Runtime.getRuntime().availableProcessors();
//        ThreadPoolExecutor.
        try {
            for (int i = 0; i < 10; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " \t 正在处理业务");
                });
                TimeUnit.MILLISECONDS.sleep(200);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
